package win.liumian.sso.server.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.liumian.sso.server.po.ResponsePo;
import win.liumian.sso.server.service.TicketService;

import static win.liumian.sso.server.util.Constants.SSO_TICKET;

/**
 * 检查用户登录凭证
 *
 * @author liumian
 */
@RestController
public class AuthController {

    private static Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private TicketService ticketService;

    /**
     * 返回用户信息
     *
     * @param requestBody
     * @return
     */
    @RequestMapping("auth")
    public ResponsePo auth(@RequestBody JSONObject requestBody) {

        String ticket = requestBody.getString(SSO_TICKET);

        boolean legalTicket = ticketService.isLegalTicket(ticket);
        JSONObject responseData = new JSONObject();
        logger.info("auth ssoTicket:{},isLegal:{}", ticket, legalTicket);
        if (legalTicket) {
            responseData.put("isLogin", true);
        } else {
            responseData.put("isLogin", false);
        }

        return new ResponsePo(true, "ok", responseData);
    }


}
